Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var addCurrencyStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=Currency',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) ,
autoLoad:true
}); 

var addDebtTypeLevelTwoStore=new Ext.data.Store({
	reader:new Ext.data.JsonReader({},[{name:'name'},{name:'code'}]),
});
var addDebtTypeLevelOneStore=new Ext.data.Store({
	proxy:new Ext.data.HttpProxy({
		url:'showDebtTypeLevelOne.do',
		method:'GET'
	}),
	reader:new Ext.data.JsonReader({},[{name:'name'},{name:'code'}]),
});
addDebtTypeLevelOneStore.load({ //加载下拉框的store
	callback:function(r,options,success){
		var levelOneValue = Ext.getCmp("debtTypeLevelOne").getValue();
		Ext.getCmp("debtTypeLevelOne").setValue(levelOneValue);
		
	}
});


var addBusinessKindStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=CreditType',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
addBusinessKindStore.load({ //加载下拉框的store
	callback:function(r,options,success){
	Ext.getCmp("businessKind").setValue(Ext.getCmp("businessKind").getValue());
}
});

var addBusinessStyleStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=BusinessStyle',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
	}); 
addBusinessStyleStore.load({ //加载下拉框的store
	callback:function(r,options,success){
	Ext.getCmp("businessStyle").setValue(Ext.getCmp("businessStyle").getValue());
}
});

var addGuarantyKindStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=GuarantyKind',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
addGuarantyKindStore.load({ //加载下拉框的store
	callback:function(r,options,success){
	Ext.getCmp("guarantyKind").setValue(Ext.getCmp("guarantyKind").getValue());
}
});

var pInterestTypeStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=PInterestType',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
	}); 

pInterestTypeStore.load({ //加载下拉框的store
	callback:function(r,options,success){
	Ext.getCmp("pinterestType").setValue(Ext.getCmp("pinterestType").getValue());
}
});

var yesOrNoStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=YesNo',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])
}); 
yesOrNoStore.load({ //加载下拉框的store
	callback:function(r,options,success){
	Ext.getCmp("groupLoanType").setValue(Ext.getCmp("groupLoanType").getValue());
}
});

var addInterestCycleStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=InterestCycleType',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])}); 
addInterestCycleStore.load({ //加载下拉框的store
	callback:function(r,options,success){
	Ext.getCmp("interestCycle").setValue(Ext.getCmp("interestCycle").getValue());
}
});




//债项详情
var addForm = new Ext.form.FormPanel({ 
id: 'addForm', 
title:'债项信息',
renderTo:"list",
height: 520, 
//样式 
bodyStyle: 'margin:5px;text-align:left;padding:5px 60px;', 
frame: true, 
xtype: 'filedset', 
labelWidth: 100, 
items: 
[ { xtype: 'fieldset',  
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[
{layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'textfield', 
	id:'debtId',
	name: 'debtId', 
	emptyText: '必填', 
	blankText: '债项 编号不能为空', 
	fieldLabel: '债项编号', 
	width: 200
}
]},{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'textfield', 
	id:'debtDesc',
	name: 'debtDesc', 
	fieldLabel: '债项名称', 
	emptyText: '必填', 
	blankText: '债项名称不能为空', 
	allowBlank: false, 
	width: 200
}
]} 
]},{
layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'textfield', 
	id:'custId',
	name: 'custId', 
	fieldLabel: '客户号', 
	readOnly:true,
	width: 200,
	hidden: true
}
]},{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'textfield', 
	id:'custName',
	name: 'custName', 
	fieldLabel: '客户名称', 
	readOnly:true,
	width: 200 ,
	hidden: true
}
]} 
]},
{layout:"column",
	items:[{
	columnWidth : .5, // 该列有整行中所占百分比	
	layout : "form",
	items : [{
		xtype: 'combo', 
		id : "debtTypeLevelOne",
		store :addDebtTypeLevelOneStore,
		fieldLabel: '债项业务品种大类',
		valueField : 'code',
		displayField : 'name',
		typeAhead : true,
		width: 200,
		mode:'remote', 
		emptyText : '请选择债项业务品种大类...',
		blankText: '债项大类不能为空', 
		allowBlank: false, 
		editable: false,
		triggerAction : 'all',
		selectOnFocus : true,
		hiddenName:'debtTypeLevelOne',
		listeners:{
			select:function(combo,record,index)
			{
				var levelTwoCombo = Ext.getCmp('debtType');
				levelTwoCombo.clearValue();
				addDebtTypeLevelTwoStore.proxy = new Ext.data.HttpProxy({
					url:'showDebtTypeLevelTwo.do?levelOneCode=' + combo.value,
					method:'GET'
				});
				addDebtTypeLevelTwoStore.reload();
			}
		}
	}]}
	,{
		columnWidth : .5, // 该列有整行中所占百分比	
		layout : "form",
		items : [{
			
	}
	]}
]},
{
	layout : "form",
	items : [{
		xtype: 'combo', 
		id : "debtType",
		store :addDebtTypeLevelTwoStore,
		fieldLabel: '债项业务品种子类',
		valueField : 'code',
		displayField : 'name',
		typeAhead : true,
		width: 380,
		mode:'remote', 
		emptyText : '请选择债项业务品种子类...',
		blankText: '债项子类不能为空', 
		allowBlank: false, 
		editable: false,
		triggerAction : 'all',
		selectOnFocus : true,
		hiddenName:'debtType',
}]
	}
,{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	xtype: 'textfield',
        	id: 'debtContractNo', 
        	name: 'debtContractNo', 
        	fieldLabel: '借款合同编号',
        	editable: true,  
        	width: 200 
        }]
    },{ 
        columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	xtype: 'textfield',
        	id: 'loanNo', 
        	name: 'loanNo', 
        	fieldLabel: '借据编号',
        	editable: true, 
        	width: 200 
        }]
   }]
},
{
layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'textfield', 
	id: 'debtSum',
	name: 'debtSum', 
	fieldLabel: '合同金额（元）', 
	emptyText: '必填', 
	blankText: '合同金额不能为空', 
	allowBlank: false,  
	regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
 	regexText:'只能输入整数或小数！' , 
	width: 200
}
]},{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
	xtype: 'combo', 
 	id : "currency",
              store :addCurrencyStore,
              fieldLabel: '债项币种',
              valueField : 'code',
              displayField : 'name',
              typeAhead : true,
              width: 200,
              mode:'remote', 
              emptyText : '请选择币种...',
              editable: false,
              triggerAction : 'all',
              selectOnFocus : true,
              hiddenName:'currency'
}
]} 
]},{
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
		       		xtype: 'textfield', 
		    		id: 'debtBalance',
		    		name: 'debtBalance', 
		    		fieldLabel: '债项余额（元）', 
		    		emptyText: '必填', 
		    		blankText: '债项余额不能为空',  
		    		allowBlank: false, 
		    		regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
		    	 	regexText:'只能输入整数或小数！' , 
		    		width: 200
	}
	]},{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
		       		xtype: 'textfield', 
		    		id: 'loanSum',
		    		name: 'loanSum', 
		    		fieldLabel: '借据金额（元）', 
		    		emptyText: ' ', 
		    		blankText: ' ', 
		    		allowBlank: true,  
		    		regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
		    	 	regexText:'只能输入整数或小数！' , 
		    		width: 200
	}
	]} 
	]},
{
layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
           xtype: 'datefield',
id: 'startDate', 
name: 'startDate', 
fieldLabel: '起始日',
allowBlank: true,
editable:false,  
format : 'Y-m-d' ,
width: 200
}
]},{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
           xtype: 'datefield',
id: 'endDate', 
name: 'endDate', 
fieldLabel: '到期日',
allowBlank: true,
editable:false,  
format : 'Y-m-d' ,
width: 200
}
]} 
]},

{
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
		layout : "form",
		items : [	{ 
			  xtype: 'combo', 
			  id : "businessKind",
		      store :addBusinessKindStore,
		      fieldLabel: '贷款性质',
		      valueField : 'code',
		      displayField : 'name',
		      typeAhead : true,
		      width: 200,
		      mode:'remote', 
		      emptyText : '请选择贷款性质...',
		      editable: false,
		      triggerAction : 'all',
		      selectOnFocus : true,
		      hiddenName:'businessKind'
		}]},{
		columnWidth : .5, // 该列有整行中所占百分比
		layout : "form",
		items : [	{ 
			xtype: 'combo', 
			  id : "businessStyle",
		      store :addBusinessStyleStore,
		      fieldLabel: '授信形式',
		      valueField : 'code',
		      displayField : 'name',
		      typeAhead : true,
		      width: 200,
		      mode:'remote', 
		      emptyText : '请选择授信形式...',
		      editable: false,
		      triggerAction : 'all',
		      selectOnFocus : true,
		      hiddenName:'businessStyle'
		}]} 
	]},

	{
		layout:"column",
		items:[{
			columnWidth : .5, // 该列有整行中所占百分比
			layout : "form",
			items : [	{ 
					xtype: 'combo', 
					id : "groupLoanType",
		              store :yesOrNoStore,
		              fieldLabel: '是否为银团贷款',
		              valueField : 'code',
		              displayField : 'name',
		              typeAhead : true,
		              width: 200,
		              mode:'remote', 
		              emptyText : '请选择是否为银团贷款...',
		              editable: false,
		              triggerAction : 'all',
		              selectOnFocus : true,
		              hiddenName:'groupLoanType'
			}]},{
			columnWidth : .5, // 该列有整行中所占百分比
			layout : "form",
			items : [	{ 
				  xtype: 'combo', 
				  id : "guarantyKind",
	              store :addGuarantyKindStore,
	              fieldLabel: '担保类型',
	              valueField : 'code',
	              displayField : 'name',
	              emptyText : '请选择担保类型...',
	              editable: false,
	              typeAhead : true,
	              width: 200,
	              mode:'remote',  
	              triggerAction : 'all',
	              selectOnFocus : true,
	              hiddenName:'guarantyKind'
			}]} 
		]},

{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
		layout : "form",
        items : [{
        	xtype: 'textfield', 
			id: 'interestSum',
			name: 'interestSum', 
			fieldLabel: '利息总额（元）', 
			width:200,
			regex:/^[0-9]+\.{0,1}[0-9]{0,5}$/,
			regexText:'只能输入整数或小数（精确度0.00001）' , 
	      
}]},{
    	columnWidth : .5, // 该列有整行中所占百分比
    	layout : "form",
        items : [{
     	   xtype: 'combo', 
     	   id : "interestCycle",
           store :addInterestCycleStore,
           fieldLabel: '计息周期',
           valueField : 'code',
           displayField : 'name',
           typeAhead : true,
           width: 200,
           mode:'remote', 
           emptyText : '请选择计息周期...',
           editable: false,
           triggerAction : 'all',
           selectOnFocus : true,
           hiddenName:'interestCycle'
           
}]} 
]},


{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
		layout : "form",
        items : [{
        	xtype: 'textfield', 
			id: 'interestRate',
			name: 'interestRate', 
			fieldLabel: '利率值（%）', 
			width: 200,	
			regex:/^[0-9]+(.[0-9]{0,5})?$/,
		 	regexText:'只能输入整数或小数（精确度0.00001）' , 
           
}]},{
    	columnWidth : .5, // 该列有整行中所占百分比
    	layout : "form",
        items : [{
        	xtype: 'textfield', 
			id: 'interestRateCode',
			name: 'interestRateCode', 
			fieldLabel: '利率代码', 
			width: 200 
           
}]} 
]},


{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	xtype: 'textfield', 
			id: 'pinterestRate',
			name: 'pinterestRate', 
			fieldLabel: '罚息利率值（%）', 
			width: 200,	
			regex:/^[0-9]+(.[0-9]{0,5})?$/,
		 	regexText:'只能输入整数或小数（精确度0.00001）' , 
			}
    ]},{
    	columnWidth : .5, // 该列有整行中所占百分比
    	layout : "form",
    	items : [{
    		xtype: 'combo', 
     	   id : "pinterestType",
           store :pInterestTypeStore,
           fieldLabel: '罚息利率类型',
           valueField : 'code',
           displayField : 'name',
           typeAhead : true,
           width: 200,
           mode:'remote', 
           emptyText : '请选择罚息利率类型...',
           editable: false,
           triggerAction : 'all',
           selectOnFocus : true,
           hiddenName:'pinterestType',
        	  
    	   }
    ]} 
]},
{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
		layout : "form",
        items : [{
        	xtype: 'textfield', 
			id: 'pinterestRateCode',
			name: 'pinterestRateCode', 
			fieldLabel: '罚息利率代码', 
			blankText: '罚息利率代码不能为空', 
			width: 200
           
}]},{
    	columnWidth : .5, // 该列有整行中所占百分比
    	layout : "form",
    	items : [{
    	   xtype: 'textfield', 
    	   id : "marginRate",
    	   fieldLabel: '保证金比例(%)',
    	   width: 200,
    	   hiddenName:'marginRate',	
			regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
		 	regexText:'只能输入整数或小数！' , 
    	   }
    ]} 
]},
{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
		layout : "form",
        items : [{
        	xtype: 'textfield', 
			id: 'collateralRate',
			name: 'collateralRate', 
			fieldLabel: '抵质押率（%）', 
			regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
		 	regexText:'只能输入整数或小数！' , 
			blankText: ' ', 
			width: 200
           
}]},{
    	columnWidth : .5, // 该列有整行中所占百分比
    	layout : "form",
    	items : [{
    	   xtype: 'textfield', 
    	   id : "guarantorNum",
    	   fieldLabel: '保证人个数',
    	   width: 200,
    	   hiddenName:'guarantorNum',	
			regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
		 	regexText:'只能输入整数 ！' , 
    	   }
    ]} 
]},
{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
		layout : "form",
        items : [{
        	xtype: 'textfield', 
			id: 'collateralTypeNum',
			name: 'collateralTypeNum', 
			fieldLabel: '同一借据下抵质押品种类数', 
			regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
		 	regexText:'只能输入整数！' , 
			blankText: ' ', 
			width: 200
           
}]},{
    	columnWidth : .5, // 该列有整行中所占百分比
    	layout : "form",
    	items : [{
    		columnWidth : .5, // 该列有整行中所占百分比
            layout : "form",
            items : [{
 xtype: 'textfield', 
 	id:'debtNo',
 	name: 'debtNo', 
 	fieldLabel: '债项编号', 
 	readOnly:true,
 	width: 200,
 	hidden: true, 
 }
 ]}
    ]} 
]}


] 
}],
buttonAlign:'right',
buttons: 
[  

{ text: '保存', handler: editFunction },
{ text: '下一步', handler: editFunctionAndNext } 
] 
});


//表单加载数据
addForm.form.load({  
    waitMsg : '正在加载数据请稍后',
    waitTitle : '提示',
    url : 'obligation/getDebtInfo.do', 
    method:'post',
    success:function(form,action){
		var data = action.result.data;
		addDebtTypeLevelTwoStore.proxy = new Ext.data.HttpProxy({
			url:'showDebtTypeLevelTwo.do?levelOneCode=' + data.debtTypeLevelOne,
			method:'GET'
		});
		addDebtTypeLevelTwoStore.reload({ //加载下拉框的store
			callback:function(r,options,success){
			Ext.getCmp("debtType").setValue(Ext.getCmp("debtType").getValue());
			}
		});
    },  
    failure:function(form,action){
    	Ext.Msg.confirm("提示",'请先添加债项基本信息！',function(bool){
			if(bool=='yes'){
				window.location.href="obligation/addDebtDetail.do";
				return true;
			}
			else{
				return false;	
			}
		}); 		
    }  
});

function editFunction(){

	if(addForm.form.isValid()){
		addForm.form.doAction('submit', { 
			url : 'obligation/editDebt.do', 
			method : 'post', 
			success : function() {
				Ext.Msg.alert('提示', '保存成功！'); 
			}, 
			failure : function() { 
				Ext.Msg.alert('提示', '保存失败！'); 
				
			} 
		}); 
	}
	
}

function editFunctionAndNext(){
	if(addForm.form.isValid()){
		addForm.form.doAction('submit', { 
			url : 'obligation/editDebt.do', 
			method : 'post', 
			success : function() {
				Ext.Msg.confirm("Confirm",'保存成功！<br>是否继续添加风险缓释信息?',function(bool){
					if(bool=='yes'){
						window.location.href="view/obligation/DebtGuaranteeInfo.jsp";
						return true;
					}
					else{
						return false;	
					}
				}); 
				//alert("成功");
				
			}, 
			failure : function() { 
				Ext.Msg.alert('提示', '保存失败！'); 
			} 
		}); 
	}
}
       
}); 